/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: deserializer.h
 *
 * Purpose: deserilizer implmentation
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __DESERIALIZER_H__
#define __DESERIALIZER_H__
#include <vector>
#include <string>
#include <array>
#include <map>
#include <stdint.h>
#include <memory>

#include "deserializable.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class Deserializer
{
public:
    using Payload = std::vector<uint8_t>;
    using PayloadPtr = std::shared_ptr<Payload>;
public:
    Deserializer(const uint8_t* data, uint32_t length, uint32_t start_pos = 0);
    Deserializer(PayloadPtr payload, uint32_t start_pos = 0);
    virtual ~Deserializer();

public:
    virtual bool Deserialize(bool& value);
    virtual bool Deserialize(uint8_t& value);
    virtual bool Deserialize(uint16_t& value);
    virtual bool Deserialize(uint32_t& value);
    virtual bool Deserialize(uint64_t& value);
    virtual bool Deserialize(int8_t& value);
    virtual bool Deserialize(int16_t& value);
    virtual bool Deserialize(int32_t& value);
    virtual bool Deserialize(int64_t& value);
    virtual bool Deserialize(float& value);
    virtual bool Deserialize(double& value);
    virtual bool Deserialize(std::string& value);
    virtual bool Deserialize(Deserializable& deser);
    virtual bool Deserialize(uint8_t* data, uint32_t size);
public:

    template<typename T>
    bool Deserialize(std::vector<T>& val)
    {/** treat vector as dynamic array, |val.size()| element values...| */
        uint32_t size;
        if (!Deserialize(size))
        {
            return false;
        }

        for (size_t i; i < size; i++)
        {
            T tmp;
            if (Deserialize(tmp))
            {
                val.push_back(std::move(tmp));
            }
            else
            {
                val.clear();
                return false;
            }
        }

        return true;
    }

    template<typename T, std::size_t N>
    bool Deserialize(std::array<T, N>& val)
    {
        uint32_t size;
        if (!Deserialize(size))
        {
            return false;
        }

        if (size <= N)
        {
            for (size_t i; i < size; i++)
            {
                T tmp;
                if (Deserialize(tmp))
                {
                    val.push_back(std::move(tmp));
                }
                else
                {
                    val.clear();
                    return false;
                }
            }
        }

        return true;
    }

    template<typename Key, typename Value>
    bool Deserialize(std::map<Key, Value>& val)
    {
        bool ret = false;
        uint32_t size;
        if (Deserialize(size))
        {
            ret = true;
            for (size_t i; i < size; i++)
            {
                Key tmp_key;
                if (Deserialize(tmp))
                {
                    Value tmp_val;
                    val[tmp_key] = std::move(tmp_val);
                }
                else
                {
                    ret = false;
                    val.clear();
                    break;
                }
            }
        }

        return ret;      
    }

    template<typename T, typename std::enable_if<std::is_enum<T>::value >::type >
    bool Deserialize(T& val) /** write for enum */
    {
        return Deserialize((typename internal_impl::enum2Int<sizeof(T)>::type&)(val));
    }

    

protected:
    PayloadPtr payload_;
    uint32_t pos_ = 0;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
using DeserializerPtr = std::shared_ptr<Deserializer>;


#endif /** !__DESERIALIZER_H__ */

